﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using MM.DBUtility;

namespace MobileManager
{
    public partial class MdoubleNumberManage : System.Web.UI.Page
    {
        private MM.Model.T_Users userModel;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["CurUser"] == null)
            {
                Response.Redirect("Login.aspx");
                return;
            }
            else
            {
                userModel = (MM.Model.T_Users)Session["CurUser"];
            }
            if (!IsPostBack)
            {
                GVDataBind();
            }
        }
        #region GV Bind
        private void GVDataBind()
        {
            string strSQL = "SELECT * FROM T_DoubleNumber WHERE 1=1 ";

            string strWhere = string.Empty;
            if (!string.IsNullOrEmpty(txtNumber.Text.Trim()))
                strWhere += string.Format(" AND TextValue='{0}'", txtNumber.Text.Trim());

            strSQL += strWhere;

            strSQL += " ORDER BY CreateTime DESC";


            DataSet ds = DbHelperSql.Query(strSQL);
            GVinfo.DataSource = ds;
            GVinfo.DataBind();
            ViewState["DataTemp"] = ds;
            lbCount.Text = "记录数：" + ds.Tables[0].Rows.Count;

        }
        private void GVDataBindAll()
        {
            string strSQL = "SELECT * FROM T_DoubleNumber ORDER BY CreateTime DESC";


            DataSet ds = DbHelperSql.Query(strSQL);
            GVinfo.DataSource = ds;
            GVinfo.DataBind();
            ViewState["DataTemp"] = ds;
            lbCount.Text = "记录数：" + ds.Tables[0].Rows.Count;

        }
        protected void GVinfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GVinfo.PageIndex = e.NewPageIndex;
            GVinfo.DataSource = (DataSet)ViewState["DataTemp"];
            GVinfo.DataBind();
        }
        #endregion

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                GVDataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void btnQueryAll_Click(object sender, EventArgs e)
        {
            try
            {
                GVDataBindAll();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                string strSQL = "Delete FROM T_DoubleNumber Where 1=1";

                if (!string.IsNullOrEmpty(txtNumber.Text.Trim()))
                    strSQL += string.Format(" AND TextValue='{0}'", txtNumber.Text.Trim());

                DbHelperSql.ExecuteSql(strSQL);

                MM.Common.Common.PageStartAlert(this.Page, "删除成功！");

                GVDataBindAll();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void btnADD_Click(object sender, EventArgs e)
        {
            try
            {
                string strNumber = txtNumber.Text.Trim();
                if (!string.IsNullOrEmpty(strNumber))
                {
                    if (DbHelperSql.Exists(string.Format("SELECT ID FROM T_DoubleNumber WHERE TextValue='{0}'", strNumber)))
                    {
                        MM.Common.Common.PageStartAlert(this.Page, "该二次号码关键字已经存在，请重新添加！");
                    }
                    else
                    {
                        DbHelperSql.ExecuteSql(string.Format("INSERT INTO T_DoubleNumber(TextValue) VALUES('{0}')", strNumber));
                        MM.Common.Common.PageStartAlert(this.Page, "添加成功！");
                        GVDataBindAll();
                    }
                }
                else MM.Common.Common.PageStartAlert(this.Page, "二次号码关键字必须填写！");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
